package weaver.jobs.cyjt.duss.cron;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.jobs.cyjt.duss.utils.KingdeeUtil;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dusongsong
 * @date 2024年11月20日 09:30
 * description:调用金蝶接口，同步基础信息到建模表单
 */
public class WeaSyncKingdeeBaseInfoCronJob extends BaseCronJob {
    @Override
    public void execute() {
        KingdeeUtil kingdeeUtil = new KingdeeUtil();
        BaseBean baseBean = new BaseBean();
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        try {
            String curdate = DateUtil.getFullDate();
            baseBean.writeLog(">>>>>WeaSyncKingdeeBaseInfoCronJob>>>>>curdate=" + curdate);
            //查询接口状态为启用的接口
            rs.executeQuery("select * from uf_jdbwpzb where jkzt=0 and ywlx=1");
            baseBean.writeLog(">>>>>WeaSyncKingdeeBaseInfoCronJob>>>>>接口数量=" + rs.getCounts());
            while (rs.next()) {
                String mainid = Util.null2String(rs.getString("id"));//id
                String jkbm = Util.null2String(rs.getString("jkbm"));//接口编码
                String qqdz = Util.null2String(rs.getString("qqdz"));//请求地址
                String dybd = Util.null2String(rs.getString("dybd"));//对应表单
                String qqfs = Util.null2String(rs.getString("qqfs"));//请求方式
                Map<String, String> fieldMap = new HashMap<>();
                rs1.executeQuery("select qqcsz from uf_jdbwpzb_dt1 where mainid=?", mainid);
                rs1.next();
                String qqcs = Util.null2String(rs1.getString("qqcsz"));
                baseBean.writeLog(">>>>>" + jkbm + ">>>>>qqcs=" + qqcs);
                rs1.executeQuery("select * from uf_jdbwpzb_dt2 where mainid=?", mainid);
                while (rs1.next()) {
                    String oazd = Util.null2String(rs1.getString("oazd"));
                    String jdzd = Util.null2String(rs1.getString("jdzd"));
                    fieldMap.put(oazd, jdzd);
                }
                baseBean.writeLog(">>>>>" + jkbm + ">>>>>fieldMap=" + fieldMap);
                if ("0".equals(qqfs)) {//GET请求
                    int pageSize = 2000;
                    int totalCount = 0;
                    if (totalCount == 0) {//默认查询第一页的数据
                        baseBean.writeLog(">>>>>" + jkbm + ">>>>>查询第1页的数据<br/>");
                        String paramStr = "pageSize=" + pageSize + "&pageNo=1";
                        baseBean.writeLog(">>>>>" + jkbm + ">>>>>paramStr=" + paramStr);
                        JSONObject resultJson = kingdeeUtil.sendOKHttpGetRequest(qqdz + "?" + paramStr);
                        String message = Util.null2String(resultJson.getString("message"));
                        if ("success".equals(message)) {
                            String datas = Util.null2String(resultJson.getString("datas"));
                            JSONObject datasJson = JSONObject.parseObject(datas);
                            String errorCode = datasJson.getString("errorCode");
                            if ("0".equals(errorCode)) {
                                JSONObject dataJson = datasJson.getJSONObject("data");
                                JSONArray rowsList = dataJson.getJSONArray("rows");
                                totalCount = dataJson.getIntValue("totalCount");
                                int insertCount = 0;
                                int updateCount = 0;
                                String dataResult = kingdeeUtil.isExistData("wyid", dybd);
                                List<String> dataList = Arrays.asList(dataResult.split(","));
                                for (int i = 0; i < rowsList.size(); i++) {
                                    Map<String, Object> map = new HashMap<>();
                                    JSONObject rowJson = rowsList.getJSONObject(i);
                                    String id = Util.null2String(rowJson.getString("id"));
                                    if (!dataList.contains(id)) {
                                        map.put("wyid", id);
                                        map.put("rqsj", curdate);
                                        boolean hasDtldata = false;
                                        for (Map.Entry<String, String> entry : fieldMap.entrySet()) {
                                            String oazd = entry.getKey();
                                            String jdzd = entry.getValue();
                                            if ("entry_bank".equals(oazd)) {
                                                hasDtldata = true;
                                            } else {
                                                map.put(oazd, Util.null2String(rowJson.getString(jdzd)));
                                            }
                                        }
                                        int dataid = kingdeeUtil.insertModelData(dybd, map);
                                        if (dataid > 0) {
                                            insertCount++;
                                            if (hasDtldata) {
                                                String entry_bank = Util.null2String(rowJson.getString("entry_bank"));
                                                if (!"".equals(entry_bank)) {
                                                    JSONArray entry_bank_arr = JSONArray.parseArray(entry_bank);
                                                    for (int j = 0; j < entry_bank_arr.size(); j++) {
                                                        Map<String, Object> dtlmap = new HashMap<>();
                                                        JSONObject entry_bank_json = entry_bank_arr.getJSONObject(j);
                                                        for (Object key : entry_bank_json.keySet()) {
                                                            dtlmap.put(Util.null2String(key), entry_bank_json.get(key));
                                                        }
                                                        dtlmap.put("mainid", dataid);
                                                        dtlmap.put("wyid", entry_bank_json.get("id"));
                                                        dtlmap.remove("id");
                                                        kingdeeUtil.insertDetailData(dybd + "_dt1", dtlmap);
                                                    }
                                                }
                                            }
                                        }
                                    } else {
                                        boolean hasDtldata = false;
                                        for (Map.Entry<String, String> entry : fieldMap.entrySet()) {
                                            String oazd = entry.getKey();
                                            String jdzd = entry.getValue();
                                            if ("entry_bank".equals(oazd)) {
                                                hasDtldata = true;
                                            } else {
                                                map.put(oazd, Util.null2String(rowJson.getString(jdzd)));
                                            }
                                        }
                                        int dataid = kingdeeUtil.updateModelData(dybd, id, map);
                                        if (dataid > 0) {
                                            updateCount++;
                                            if (hasDtldata) {
                                                String entry_bank = Util.null2String(rowJson.getString("entry_bank"));
                                                if (!"".equals(entry_bank)) {
                                                    kingdeeUtil.deleteDetailData(dybd + "_dt1", dataid);
                                                    JSONArray entry_bank_arr = JSONArray.parseArray(entry_bank);
                                                    for (int j = 0; j < entry_bank_arr.size(); j++) {
                                                        Map<String, Object> dtlmap = new HashMap<>();
                                                        JSONObject entry_bank_json = entry_bank_arr.getJSONObject(j);
                                                        for (Object key : entry_bank_json.keySet()) {
                                                            dtlmap.put(Util.null2String(key), entry_bank_json.get(key));
                                                        }
                                                        dtlmap.put("mainid", dataid);
                                                        dtlmap.put("wyid", entry_bank_json.get("id"));
                                                        dtlmap.remove("id");
                                                        kingdeeUtil.insertDetailData(dybd + "_dt1", dtlmap);

                                                    }
                                                }
                                            }
                                        }

                                    }
                                }
                                baseBean.writeLog(">>>>>" + jkbm + ">>>>>总数量：[" + totalCount + "]<br/>");
                                baseBean.writeLog(">>>>>" + jkbm + ">>>>>插入数量：[" + insertCount + "]<br/>");
                                baseBean.writeLog(">>>>>" + jkbm + ">>>>>更新数量：[" + updateCount + "]<br/>");
                            } else {
                                baseBean.writeLog(">>>>>" + jkbm + ">>>>>接口异常返回：" + datas);
                            }
                        }
                    }

                    if (totalCount > pageSize) {//默认查询后几页的数据
                        int count = (int) Math.ceil(totalCount / pageSize);
                        for (int i = 1; i <= count; i++) {
                            baseBean.writeLog(">>>>>" + jkbm + ">>>>>查询第" + (i + 1) + "页的数据<br/>");
                            String paramStr = "pageSize=" + pageSize + "&pageNo=" + (i + 1);
                            baseBean.writeLog(">>>>>" + jkbm + ">>>>>paramStr=" + paramStr);
                            JSONObject resultJson = kingdeeUtil.sendOKHttpGetRequest(qqdz + "?" + paramStr);
                            String message = Util.null2String(resultJson.getString("message"));
                            if ("success".equals(message)) {
                                String datas = Util.null2String(resultJson.getString("datas"));
                                JSONObject datasJson = JSONObject.parseObject(datas);
                                String errorCode = datasJson.getString("errorCode");
                                if ("0".equals(errorCode)) {
                                    JSONObject dataJson = datasJson.getJSONObject("data");
                                    JSONArray rowsList = dataJson.getJSONArray("rows");
                                    int insertCount = 0;
                                    int updateCount = 0;
                                    String dataResult = kingdeeUtil.isExistData("wyid", dybd);
                                    List<String> dataList = Arrays.asList(dataResult.split(","));
                                    for (int j = 0; j < rowsList.size(); j++) {
                                        Map<String, Object> map = new HashMap<>();
                                        JSONObject rowJson = rowsList.getJSONObject(j);
                                        String id = Util.null2String(rowJson.getString("id"));
                                        if (!dataList.contains(id)) {
                                            map.put("wyid", id);
                                            map.put("rqsj", curdate);
                                            boolean hasDtldata = false;
                                            for (Map.Entry<String, String> entry : fieldMap.entrySet()) {
                                                String oazd = entry.getKey();
                                                String jdzd = entry.getValue();
                                                if ("entry_bank".equals(oazd)) {
                                                    hasDtldata = true;
                                                } else {
                                                    map.put(oazd, Util.null2String(rowJson.getString(jdzd)));
                                                }
                                            }
                                            int dataid = kingdeeUtil.insertModelData(dybd, map);
                                            if (dataid > 0) {
                                                insertCount++;
                                                if (hasDtldata) {
                                                    String entry_bank = Util.null2String(rowJson.getString("entry_bank"));
                                                    if (!"".equals(entry_bank)) {
                                                        JSONArray entry_bank_arr = JSONArray.parseArray(entry_bank);
                                                        for (int k = 0; k < entry_bank_arr.size(); k++) {
                                                            Map<String, Object> dtlmap = new HashMap<>();
                                                            JSONObject entry_bank_json = entry_bank_arr.getJSONObject(k);
                                                            for (Object key : entry_bank_json.keySet()) {
                                                                dtlmap.put(Util.null2String(key), entry_bank_json.get(key));
                                                            }
                                                            dtlmap.put("mainid", dataid);
                                                            dtlmap.put("wyid", entry_bank_json.get("id"));
                                                            dtlmap.remove("id");
                                                            kingdeeUtil.insertDetailData(dybd + "_dt1", dtlmap);
                                                        }
                                                    }
                                                }
                                            }
                                        } else {
                                            boolean hasDtldata = false;
                                            for (Map.Entry<String, String> entry : fieldMap.entrySet()) {
                                                String oazd = entry.getKey();
                                                String jdzd = entry.getValue();
                                                if ("entry_bank".equals(oazd)) {
                                                    hasDtldata = true;
                                                } else {
                                                    map.put(oazd, Util.null2String(rowJson.getString(jdzd)));
                                                }
                                            }
                                            int dataid = kingdeeUtil.updateModelData(dybd, id, map);
                                            if (dataid > 0) {
                                                updateCount++;
                                                if (hasDtldata) {
                                                    String entry_bank = Util.null2String(rowJson.getString("entry_bank"));
                                                    if (!"".equals(entry_bank)) {
                                                        kingdeeUtil.deleteDetailData(dybd + "_dt1", dataid);
                                                        JSONArray entry_bank_arr = JSONArray.parseArray(entry_bank);
                                                        for (int k = 0; k < entry_bank_arr.size(); k++) {
                                                            Map<String, Object> dtlmap = new HashMap<>();
                                                            JSONObject entry_bank_json = entry_bank_arr.getJSONObject(k);
                                                            for (Object key : entry_bank_json.keySet()) {
                                                                dtlmap.put(Util.null2String(key), entry_bank_json.get(key));
                                                            }
                                                            dtlmap.put("mainid", dataid);
                                                            dtlmap.put("wyid", entry_bank_json.get("id"));
                                                            dtlmap.remove("id");
                                                            kingdeeUtil.insertDetailData(dybd + "_dt1", dtlmap);
                                                        }
                                                    }
                                                }
                                            }

                                        }
                                    }
                                    baseBean.writeLog(">>>>>" + jkbm + ">>>>>总数量：[" + totalCount + "]<br/>");
                                    baseBean.writeLog(">>>>>" + jkbm + ">>>>>插入数量：[" + insertCount + "]<br/>");
                                    baseBean.writeLog(">>>>>" + jkbm + ">>>>>更新数量：[" + updateCount + "]<br/>");
                                } else {
                                    baseBean.writeLog(">>>>>" + jkbm + ">>>>>接口异常返回：" + datas);
                                }
                            }
                        }
                    }
                } else {//POST请求
                    int totalCount = 0;
                    int pageSize = 0;
                    if (totalCount == 0) {
                        baseBean.writeLog(">>>>>" + jkbm + ">>>>>查询第1页的数据<br/>");
                        JSONObject paramJson = JSONObject.parseObject(qqcs);
                        pageSize = Util.getIntValue(paramJson.getString("pageSize"));
                        baseBean.writeLog(">>>>>" + jkbm + ">>>>>paramJson=" + paramJson);
                        JSONObject resultJson = kingdeeUtil.sendOKHttpPostRequest(qqdz, paramJson.toJSONString());
                        String message = Util.null2String(resultJson.getString("message"));
                        if ("success".equals(message)) {
                            String datas = Util.null2String(resultJson.getString("datas"));
                            JSONObject datasJson = JSONObject.parseObject(datas);
                            String errorCode = datasJson.getString("errorCode");
                            if ("0".equals(errorCode)) {
                                String data = Util.null2String(datasJson.getString("data"));
                                JSONArray rowsList = new JSONArray();
                                if (kingdeeUtil.isJSONObject(data)) {
                                    JSONObject dataJson = JSONObject.parseObject(data);
                                    totalCount = Util.getIntValue(dataJson.getString("totalCount"), 0);
                                    rowsList = dataJson.getJSONArray("rows");
                                }
                                if (kingdeeUtil.isJSONArray(data)) {
                                    rowsList = JSONArray.parseArray(data);
                                }
                                int insertCount = 0;
                                int updateCount = 0;
                                String dataResult = kingdeeUtil.isExistData("wyid", dybd);
                                List<String> dataList = Arrays.asList(dataResult.split(","));
                                for (int i = 0; i < rowsList.size(); i++) {
                                    Map<String, Object> map = new HashMap<>();
                                    JSONObject rowJson = rowsList.getJSONObject(i);
                                    String id = Util.null2String(rowJson.getString("id"));
                                    if (!dataList.contains(id)) {
                                        map.put("wyid", id);
                                        map.put("rqsj", curdate);
                                        boolean hasDtldata = false;
                                        for (Map.Entry<String, String> entry : fieldMap.entrySet()) {
                                            String oazd = entry.getKey();
                                            String jdzd = entry.getValue();
                                            if ("entry_bank".equals(oazd)) {
                                                hasDtldata = true;
                                            } else {
                                                map.put(oazd, Util.null2String(rowJson.getString(jdzd)));
                                            }
                                        }
                                        int dataid = kingdeeUtil.insertModelData(dybd, map);
                                        if (dataid > 0) {
                                            insertCount++;
                                            if (hasDtldata) {
                                                String entry_bank = Util.null2String(rowJson.getString("entry_bank"));
                                                if (!"".equals(entry_bank)) {
                                                    JSONArray entry_bank_arr = JSONArray.parseArray(entry_bank);
                                                    for (int j = 0; j < entry_bank_arr.size(); j++) {
                                                        Map<String, Object> dtlmap = new HashMap<>();
                                                        JSONObject entry_bank_json = entry_bank_arr.getJSONObject(j);
                                                        for (Object key : entry_bank_json.keySet()) {
                                                            dtlmap.put(Util.null2String(key), entry_bank_json.get(key));
                                                        }
                                                        dtlmap.put("mainid", dataid);
                                                        dtlmap.put("wyid", entry_bank_json.get("id"));
                                                        dtlmap.remove("id");
                                                        kingdeeUtil.insertDetailData(dybd + "_dt1", dtlmap);
                                                    }
                                                }
                                            }

                                        }
                                    } else {
                                        boolean hasDtldata = false;
                                        for (Map.Entry<String, String> entry : fieldMap.entrySet()) {
                                            String oazd = entry.getKey();
                                            String jdzd = entry.getValue();
                                            if ("entry_bank".equals(oazd)) {
                                                hasDtldata = true;
                                            } else {
                                                map.put(oazd, Util.null2String(rowJson.getString(jdzd)));
                                            }
                                        }
                                        int dataid = kingdeeUtil.updateModelData(dybd, id, map);
                                        if (dataid > 0) {
                                            updateCount++;
                                            if (hasDtldata) {
                                                String entry_bank = Util.null2String(rowJson.getString("entry_bank"));
                                                if (!"".equals(entry_bank)) {
                                                    kingdeeUtil.deleteDetailData(dybd + "_dt1", dataid);
                                                    JSONArray entry_bank_arr = JSONArray.parseArray(entry_bank);
                                                    for (int j = 0; j < entry_bank_arr.size(); j++) {
                                                        Map<String, Object> dtlmap = new HashMap<>();
                                                        JSONObject entry_bank_json = entry_bank_arr.getJSONObject(j);
                                                        for (Object key : entry_bank_json.keySet()) {
                                                            dtlmap.put(Util.null2String(key), entry_bank_json.get(key));
                                                        }
                                                        dtlmap.put("mainid", dataid);
                                                        dtlmap.put("wyid", entry_bank_json.get("id"));
                                                        dtlmap.remove("id");
                                                        kingdeeUtil.insertDetailData(dybd + "_dt1", dtlmap);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                baseBean.writeLog(">>>>>" + jkbm + ">>>>>总数量：[" + totalCount + "]<br/>");
                                baseBean.writeLog(">>>>>" + jkbm + ">>>>>插入数量：[" + insertCount + "]<br/>");
                                baseBean.writeLog(">>>>>" + jkbm + ">>>>>更新数量：[" + updateCount + "]<br/>");
                            } else {
                                baseBean.writeLog(">>>>>" + jkbm + ">>>>>接口异常返回：" + datas);
                            }
                        }
                    }
                    if (totalCount > pageSize) {//默认查询后几页的数据
                        int count = (int) Math.ceil(totalCount / pageSize);
                        for (int i = 1; i <= count; i++) {
                            baseBean.writeLog(">>>>>" + jkbm + ">>>>>查询第" + (i + 1) + "页的数据<br/>");
                            JSONObject paramJson = JSONObject.parseObject(qqcs);
                            paramJson.put("pageSize", pageSize);
                            paramJson.put("pageNo", i + 1);
                            baseBean.writeLog(">>>>>" + jkbm + ">>>>>paramJson=" + paramJson);
                            JSONObject resultJson = kingdeeUtil.sendOKHttpPostRequest(qqdz, paramJson.toJSONString());
                            String message = Util.null2String(resultJson.getString("message"));
                            if ("success".equals(message)) {
                                String datas = Util.null2String(resultJson.getString("datas"));
                                JSONObject datasJson = JSONObject.parseObject(datas);
                                String errorCode = datasJson.getString("errorCode");
                                if ("0".equals(errorCode)) {
                                    String data = Util.null2String(datasJson.getString("data"));
                                    JSONArray rowsList = new JSONArray();
                                    if (kingdeeUtil.isJSONObject(data)) {
                                        JSONObject dataJson = JSONObject.parseObject(data);
                                        totalCount = Util.getIntValue(dataJson.getString("totalCount"), 0);
                                        rowsList = dataJson.getJSONArray("rows");
                                    }
                                    if (kingdeeUtil.isJSONArray(data)) {
                                        rowsList = JSONArray.parseArray(data);
                                    }
                                    int insertCount = 0;
                                    int updateCount = 0;
                                    String dataResult = kingdeeUtil.isExistData("wyid", dybd);
                                    List<String> dataList = Arrays.asList(dataResult.split(","));
                                    for (int j = 0; j < rowsList.size(); j++) {
                                        Map<String, Object> map = new HashMap<>();
                                        JSONObject rowJson = rowsList.getJSONObject(j);
                                        String id = Util.null2String(rowJson.getString("id"));
                                        if (!dataList.contains(id)) {
                                            map.put("wyid", id);
                                            map.put("rqsj", curdate);
                                            boolean hasDtldata = false;
                                            for (Map.Entry<String, String> entry : fieldMap.entrySet()) {
                                                String oazd = entry.getKey();
                                                String jdzd = entry.getValue();
                                                if ("entry_bank".equals(oazd)) {
                                                    hasDtldata = true;
                                                } else {
                                                    map.put(oazd, Util.null2String(rowJson.getString(jdzd)));
                                                }
                                            }
                                            int dataid = kingdeeUtil.insertModelData(dybd, map);
                                            if (dataid > 0) {
                                                insertCount++;
                                                if (hasDtldata) {
                                                    String entry_bank = Util.null2String(rowJson.getString("entry_bank"));
                                                    if (!"".equals(entry_bank)) {
                                                        JSONArray entry_bank_arr = JSONArray.parseArray(entry_bank);
                                                        for (int k = 0; k < entry_bank_arr.size(); k++) {
                                                            Map<String, Object> dtlmap = new HashMap<>();
                                                            JSONObject entry_bank_json = entry_bank_arr.getJSONObject(k);
                                                            for (Object key : entry_bank_json.keySet()) {
                                                                dtlmap.put(Util.null2String(key), entry_bank_json.get(key));
                                                            }
                                                            dtlmap.put("mainid", dataid);
                                                            dtlmap.put("wyid", entry_bank_json.get("id"));
                                                            dtlmap.remove("id");
                                                            kingdeeUtil.insertDetailData(dybd + "_dt1", dtlmap);
                                                        }
                                                    }
                                                }
                                            }
                                        } else {
                                            boolean hasDtldata = false;
                                            for (Map.Entry<String, String> entry : fieldMap.entrySet()) {
                                                String oazd = entry.getKey();
                                                String jdzd = entry.getValue();
                                                if ("entry_bank".equals(oazd)) {
                                                    hasDtldata = true;
                                                } else {
                                                    map.put(oazd, Util.null2String(rowJson.getString(jdzd)));
                                                }
                                            }
                                            int dataid = kingdeeUtil.updateModelData(dybd, id, map);
                                            if (dataid > 0) {
                                                updateCount++;
                                                if (hasDtldata) {
                                                    String entry_bank = Util.null2String(rowJson.getString("entry_bank"));
                                                    if (!"".equals(entry_bank)) {
                                                        kingdeeUtil.deleteDetailData(dybd + "_dt1", dataid);
                                                        JSONArray entry_bank_arr = JSONArray.parseArray(entry_bank);
                                                        for (int k = 0; k < entry_bank_arr.size(); k++) {
                                                            Map<String, Object> dtlmap = new HashMap<>();
                                                            JSONObject entry_bank_json = entry_bank_arr.getJSONObject(k);
                                                            for (Object key : entry_bank_json.keySet()) {
                                                                dtlmap.put(Util.null2String(key), entry_bank_json.get(key));
                                                            }
                                                            dtlmap.put("mainid", dataid);
                                                            dtlmap.put("wyid", entry_bank_json.get("id"));
                                                            dtlmap.remove("id");
                                                            kingdeeUtil.insertDetailData(dybd + "_dt1", dtlmap);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    baseBean.writeLog(">>>>>" + jkbm + ">>>>>总数量：[" + totalCount + "]<br/>");
                                    baseBean.writeLog(">>>>>" + jkbm + ">>>>>插入数量：[" + insertCount + "]<br/>");
                                    baseBean.writeLog(">>>>>" + jkbm + ">>>>>更新数量：[" + updateCount + "]<br/>");
                                } else {
                                    baseBean.writeLog(">>>>>" + jkbm + ">>>>>接口异常返回：" + datas);
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            baseBean.writeLog("WeaSyncKingdeeBaseInfoCronJob" + e);
            e.printStackTrace();
        }
    }
}
